// FUNCTIONS.JS
// Utility functions that Ext JS does not provide, for use throughout the
// application.
// ----------------------------------------------------------------------------

Ext.define('Util.AccountHelper', {}); // Placeholder, ignore this.

window.AccountHelper =
{

    phoneNumberPattern : /^\(?(\d*)\)?[- .]?(\d*)[- .]?(\d+)$/,

    updateFormat : function(ctnDigites)
    {
        var newValue = "(" + ctnDigites.substr(0, 3) + ") " + ctnDigites.substr(3, 3) + "-" + ctnDigites.substr(6);
        return newValue;
    },

    validatePhoneNumber : function(value)
    {
        var res = true;
        if (!!value)
        {
            var ctnDigites = AccountHelper.getAllDigites(value);

            if (!AccountHelper.phoneNumberPattern.test(value))
            {
                res = "This phone number format is not recognized. Please check the number.";
            } else if (ctnDigites.length > 10)
            {
                res = "Please enter 10 digits";
            }
        }
        return res;
    },

    getAllDigites : function(inputValue)
    {
        var digites = "";
        for ( var i = 0; i < inputValue.length; i++)
        {
            if (inputValue.charCodeAt(i) >= 48 && inputValue.charCodeAt(i) <= 57)
            {
                digites = digites + inputValue.substr(i, 1);
            }
        }
        return digites;
    },

    formatAddress : function(arr)
    {
        var result = '';
        var delimiter = ', ';
        for ( var i in arr)
        {
            var str = arr[i].trim();
            if (!Functions.isEmpty(result) && !Functions.isEmpty(str))
            {
                result += delimiter;
            }
            if (!Functions.isEmpty(str))
            {
                result += str;
            }
        }
        return result;
    }
};
